Home /

Developer Resource Kit 5 Article

Font Browser Sample Application

The Font Browser sample application allows users to browse fonts installed on their computer. One of the main reasons we created this application is because users often complain how tedious it is to open Fireworks just to preview a font. With this application, you can preview the font easily and determine the size you want it in.

The current feature set allows you to do the following:

  • Change the sample text used to preview a font, either in "abcABC" format (the whole alphabet) or as a pangram (a sentence that uses every letter in the alphabet)
  • List all the installed fonts on your computer so that clicking a font name previews that font
  • Preview a font in two sizes
  • View a font with left-, center-, and right-alignment
  • Save your current settings so that when you open the application later, you don't have to reset the options
Font Browser sample application

Figure 1. Font Browser sample application

Installing Font Browser

To install Font Browser, simply extract the SWF to your hard drive and open the file.

Learning from Font Browser

The Font Browser sample application takes advantage of Cascading Style Sheets (CSS). With CSS, all you have to do is reset the current style, instead of going through different style formats, like in Flash MX.

For example, look at the code in the fontListener function:

fontselectedVar = this.selectedItem;
fontselectedIndex = this.selectedIndex;
var styles = new TextField.StyleSheet(); 
styles.setStyle("previewOneText", 
  {fontFamily: fontselectedVar,
  fontSize: firstsize,
  textAlign: currentAlignVar}
);
styles.setStyle("previewTwoText", 
  {fontFamily: fontselectedVar,
  fontSize: secondsize,
  textAlign: currentAlignVar}
);
this._parent.fontpreview_txt.styleSheet = styles;
this._parent.fontpreview_txt.text = this._parent.fontpreview_txt.text;

Examining the code, note the following:

  • fontselectedVar = this.selectedItem defines the selected font variable to be used later.
  • fontselectedIndex = this.selectedIndex defines the selected index so that you can reset the selection.
  • var styles = new TextField.StyleSheet() creates the style sheet.
  • In the previewOneText style, we set the fontFamily property to fontselectedVar, we set fontSize to the firstsize variable (which is defined earlier in the application; this is the value of the first numeric stepper), and we set textAlign to currentAlignVar (which is also defined earlier in the application). The same thing happens in the previewTwoText style.
  • this._parent.fontpreview_txt.styleSheet = styles tells the style which text field to be applied to.
  • this._parent.fontpreview_txt.text = this._parent.fontpreview_txt.text resets the text.

As you can see, CSS plays a huge role in the creation of Font Browser by allowing you just to redefine the styles. If you want to add a style element, just modify the previewOneText or previewTwoText style. For instance, if you add a Color Picker component to the application, all you need to do to this code is add a variable defining the Color Picker value and add a color value to the CSS style (for instance, color: colorValue). This makes it very easy to extend Font Browser later if necessary.